﻿@inject IOptionsSnapshot<SiteOptions> SiteOptions
@inject Dotnet9DbContext DbContext
@inject IDistributedCacheHelper CacheHelper

@{
    const string cacheKey = "_RandomListPartial";

    async Task<List<RandomBlogPost>?> GetTagFromDb()
    {
        return await DbContext.BlogPosts!.AsNoTracking().Take(30).Select(x => new RandomBlogPost(x.CreationTime, x.Title, x.Slug)).ToListAsync();
    }

    var randomList = await CacheHelper.GetOrCreateAsync(cacheKey,
        async e => await GetTagFromDb());
}

@if (randomList is {Count:>0})

{
    <div class="whitebg suiji">
        <h2 class="side-title">猜你喜欢</h2>
        <ul>
            @foreach (var post in randomList.RandomItems(10))
            {
                <li>
                    <a target="_blank" href="@post.CreationTime.ToString("/yyyy/MM")/@post.Slug" title="@post.Title">@post.Title</a>
                </li>
            }
        </ul>
    </div>
}